const { to, toast } = require("../../utils/wxapi");
const api = require("../../api/sop");

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    item: {},
    show: Boolean,
  },

  /**
   * 组件的初始数据
   */
  data: {},

  /**
   * 组件的方法列表
   */
  methods: {
    goToContent(e) {
      const { id } = e.currentTarget.dataset;
      let history = wx.getStorageSync("history") || [];
      history.push(id);
      const newHistory = [...new Set(history)];
      wx.setStorageSync("history", newHistory);
      to("/two/pages/detail/detail?id=" + id);
    },
    async subming(e) {
      const user = wx.getStorageSync("user");
      const { id } = e.currentTarget.dataset;
      if (!user.mobile) {
        wx.showModal({
          title: "提示",
          content: "检测到您还未登录是否前往登录",
          complete: (res) => {
            if (res.confirm) {
              to("/two/login/login");
            }
          },
        });
      } else if (user.realState !== 1) {
        wx.showModal({
          title: "提示",
          content: "检测到您还未实名认证，是否前往",
          complete: (res) => {
            if (res.confirm) {
              to("/two/pages/userId/userId");
            }
          },
        });
      } else {
        const resp = await api.entryOrNot({
          userId: user.userId,
          workId: id,
        });
        if (resp.respCode === 200) {
          const data = JSON.stringify({
            userId: user.userId,
            workId: id,
          });
          const res = await api.entry(data);
          if (res.respCode === 200) {
            toast("申请成功");
            setTimeout(() => {
              const id = JSON.parse(res.source).id;
              to("/two/enlist/enlist?id=" + id);
            }, 1500);
          } else {
            toast(res.msg);
          }
        } else {
          toast(resp.msg);
        }
      }
    },
  },
});
